﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using FinalPointSet.Model.Asset;
using FinalPointSet.Model.Constant;
using FinalPointSet.Model.Constant.Message;
using FinalPointSet.Model.Constants;
using FinalPointSet.Model.Manager;
using FinalPointSet.Model.Model;
using FinalPointSet.Model.Model.FieldMapping;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using static FinalPointSet.Model.Manager.MessageManager;

namespace FinalPointSet.ViewModel.Page.FieldMapping
{
    public partial class DataSourceSettingPageViewModel : ObservableObject
    {
        /// <summary>
        /// 数据源集合
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<DataSource> _DataSources;
        /// <summary>
        /// 当前选中的数据源索引
        /// </summary>
        [ObservableProperty]
        private int _SelectedIndex = 0;
        /// <summary>
        /// 当前选中的数据源
        /// </summary>
        [ObservableProperty]
        private DataSource _DataSource = new();
        /// <summary>
        /// 选择图标弹窗是否可见
        /// </summary>
        [ObservableProperty]
        private bool _IsIconPickerVisible = false;
        /// <summary>
        /// 图标路径集合
        /// </summary>
        [ObservableProperty]
        private List<TextPair> _LogoPaths = [];
        public DataSourceSettingPageViewModel()
        {
            DataSources = new(AssetManager.Instance.GetJsonObjectList<DataSource>().OrderBy(ds => ds.Sequence));
            DataSource = DataSources[SelectedIndex];
            LogoPaths = AssetManager.Instance.GetTextList(Path.DataSourceLogo);
        }

        #region 数据源相关命令
        /// <summary>
        /// 新增
        /// </summary>
        [RelayCommand]
        private void Add()
        {
            int maxSeq = (DataSources.Count > 0) ? DataSources.Max(d => d.Sequence) + 1 : 1;
            string name = $"数据源{maxSeq}";
            DataSource dataSource = new()
            {
                Sequence = maxSeq,
                Name = name,
                LogoPath = "Assets/Images/DataSource/Default.svg"
            };
            DataSources.Add(dataSource);
            Debug.Print($"DataSourceSettingPageViewModel.Add(), 新增数据源[{dataSource.Name}]成功!");
            Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Success, $"新增数据源【{dataSource.Name}】成功!"));
        }

        /// <summary>
        /// 移除数据源
        /// </summary>
        [RelayCommand]
        private void Remove(DataSource ds)
        {
            DataSources.Remove(ds);
            Debug.Print($"DataSourceSettingPageViewModel.Remove(), 移除数据源【{ds.Name}】成功!");
            Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Success, $"移除数据源【{ds.Name}】成功!"));
        }

        /// <summary>
        /// 上移数据源
        /// </summary>
        [RelayCommand]
        private void MoveUp(DataSource ds)
        {
            if (ds == null) return;
            int index = DataSources.IndexOf(ds);
            if (index <= 0)
            {
                Debug.Print($"DataSourceSettingPageViewModel.MoveUp(), 数据源已位于最顶部, 无法移动");
                Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Warning, "数据源已位于最顶部, 无法移动"));
                return;
            }
            //交换排序索引
            (DataSources[index].Sequence, DataSources[index - 1].Sequence) = (DataSources[index - 1].Sequence, DataSources[index].Sequence);
            DataSources = new(DataSources.ToList().OrderBy(ds => ds.Sequence));
            Debug.Print($"DataSourceSettingPageViewModel.MoveUp(), 上移数据源:{ds.Name}");
        }

        /// <summary>
        /// 下移数据源
        /// </summary>
        [RelayCommand]
        private void MoveDown(DataSource ds)
        {
            if (ds == null) return;
            int index = DataSources.IndexOf(ds);
            if (index >= DataSources.Count - 1)
            {
                Debug.Print($"DataSourceSettingPageViewModel.MoveDown(), 数据源已位于最底部, 无法移动");
                Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Warning, "数据源已位于最底部, 无法移动"));
                return;
            }
            //交换排序索引
            (DataSources[index].Sequence, DataSources[index + 1].Sequence) = (DataSources[index + 1].Sequence, DataSources[index].Sequence);
            DataSources = new(DataSources.ToList().OrderBy(ds => ds.Sequence));
            Debug.Print($"DataSourceSettingPageViewModel.MoveDown(), 下移数据源:{ds.Name}");
        }

        /// <summary>
        /// 保存
        /// </summary>
        [RelayCommand]
        private void Save()
        {
            if (DataSources == null) return;
            //保存数据
            AssetManager.Instance.UpdateJsonObjectList(DataSources.ToList());
            AssetManager.Instance.SaveJsonObjectList<DataSource>();
            Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Success, $"保存数据源成功"));
        }
        #endregion

        #region 数据源字段类型相关命令
        /// <summary>
        /// 新增数据源字段类型
        /// </summary>
        [RelayCommand]
        private void AddFieldType()
        {
            if (DataSource == null)
                return;

            int maxSeq = (DataSource.FieldTypes.Count > 0) ? DataSource.FieldTypes.Max(d => d.Sequence) + 1 : 1;
            string name = $"类型{maxSeq}";
            DataSourceFieldType fieldType = new()
            {
                Sequence = maxSeq,
                Name = name,
            };
            DataSource.FieldTypes.Add(fieldType);
            Debug.Print($"DataSourceSettingPageViewModel.AddFieldType(), 新增数据源字段类型[{fieldType.Name}]成功!");
            Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Success, $"新增数据源字段类型[{fieldType.Name}]成功!"));
        }

        /// <summary>
        /// 移除数据源字段类型
        /// </summary>
        [RelayCommand]
        private void RemoveFieldType(DataSourceFieldType fieldType)
        {
            if (DataSource == null)
                return;
            DataSource.FieldTypes.Remove(fieldType);
            Debug.Print($"DataSourceSettingPageViewModel.RemoveFieldType(), 移除数据源字段类型[{fieldType.Name}]成功!");
            Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Success, $"移除数据源字段类型[{fieldType.Name}]成功!"));
        }

        /// <summary>
        /// 上移数据源字段类型
        /// </summary>
        [RelayCommand]
        private void MoveUpFieldType(DataSourceFieldType fieldType)
        {
            if (fieldType == null)
                return;
            int index = DataSource.FieldTypes.IndexOf(fieldType);
            if (index <= 0)
            {
                Debug.Print($"DataSourceSettingPageViewModel.MoveUpFieldType(), 数据源字段类型已位于最顶部, 无法移动");
                Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Warning, "数据源字段类型已位于最顶部, 无法移动"));
                return;
            }
            //交换排序索引
            (DataSource.FieldTypes[index].Sequence, DataSource.FieldTypes[index - 1].Sequence) = (DataSource.FieldTypes[index - 1].Sequence, DataSource.FieldTypes[index].Sequence);
            List<DataSourceFieldType> dataSourceFieldTypes = [.. DataSource.FieldTypes.OrderBy(ds => ds.Sequence)];
            DataSource.FieldTypes.Clear();
            foreach (DataSourceFieldType item in dataSourceFieldTypes)
            {
                DataSource.FieldTypes.Add(item);
            }
            Debug.Print($"DataSourceSettingPageViewModel.MoveUp(), 上移数据源字段类型:{fieldType.Name}");
        }

        /// <summary>
        /// 下移数据源字段类型
        /// </summary>
        [RelayCommand]
        private void MoveDownFieldType(DataSourceFieldType fieldType)
        {
            if (fieldType == null)
                return;
            int index = DataSource.FieldTypes.IndexOf(fieldType);
            if (index >= DataSource.FieldTypes.Count - 1)
            {
                Debug.Print($"DataSourceSettingPageViewModel.MoveDownFieldType(), 数据源字段类型已位于最底部, 无法移动");
                Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Warning, "数据源字段类型已位于最底部, 无法移动"));
                return;
            }
            //交换排序索引
            (DataSource.FieldTypes[index].Sequence, DataSource.FieldTypes[index + 1].Sequence) = (DataSource.FieldTypes[index + 1].Sequence, DataSource.FieldTypes[index].Sequence);
            List<DataSourceFieldType> dataSourceFieldTypes = [.. DataSource.FieldTypes.OrderBy(ds => ds.Sequence)];
            DataSource.FieldTypes.Clear();
            foreach (DataSourceFieldType item in dataSourceFieldTypes)
            {
                DataSource.FieldTypes.Add(item);
            }
            Debug.Print($"DataSourceSettingPageViewModel.MoveDownFieldType(), 下移数据源字段类型:{fieldType.Name}");
        }
        #endregion
    }
}